<template>
  <div class="video-message" :class="{ full: isFull }">
    <div class="mask" v-if="isPlay">
      <div class="toolbar">
        <p>
            <i class="iconfont icon-fullscreen" v-if="!isFull" @click="full"></i>
            <i class="iconfont icon-tuichuquanping" v-else @click="full"></i>
        </p>
        <p v-show="isFull">
            <el-icon @click="refresh"><ele-RefreshLeft /></el-icon>
        </p>
        <!-- <p v-show="isFull">工具栏</p> -->
      </div>
    </div>

    <video
      ref="video"
      preload="auto"
      :src="src"
      @ended="ended"
      @pause="onPause"
      @error="onError"
      @resize="onResize"
      @canplay="onLoadstart"
    ></video>

    <p class="play-controls" :style="{visibility: !isPlay ? 'visible' : ''}"> 
        <i @click="toPlay" class="fa fa-pause" v-if="isPlay"></i>
        <i @click="toPlay" class="fa fa-play" v-else></i>
    </p>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from 'vue'
export default {
    name: 'VideoMessage',
    props: {
        src: {
            type: String,
            default: ''
        }
    },
    setup() {
        const video = ref()
        const state = reactive({
            isPlay: false,
            isFull: false,
            error: false,
            duration: 0
        })
        const full = () => {
            state.isFull = !state.isFull
        }
        const onPause = () => {
            state.isPlay = false
        }
        const ended = () => {
            state.isPlay = false
        }
        const toPlay = () => {
            let videoDom = video.value
            if (state.error) return
            if (state.isPlay) {
                videoDom.pause()
            } else {
                videoDom.play()
            }
            state.isPlay = !state.isPlay
        }
        const onError = () => {
            state.error = true
        }
        const onResize = () => {
            console.log('onResize')
        }
        const refresh = () => {
            video.value.currentTime = 0
        }
        const onLoadstart = () => {
        
        }
        return {
            ...toRefs(state),
            full,
            onPause,
            ended,
            toPlay,
            onError,
            onResize,
            refresh,
            onLoadstart,
            video
        }
    }
}
</script>

<style lang="scss" scoped>
.video-message {
  position: relative;
  min-width: 150px;
  max-width: 80%;
  video {
    border-radius: 3px;
    overflow: hidden;
    cursor: pointer !important;
    position: relative;
    margin-bottom: 0;
    background: black;
    max-width: 300px;
    display: block;
    max-height: 300px;
    object-fit: contain;
  }
  .play-controls{
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 5;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    visibility: hidden;
    i{
      font-size: 28px;
      color: white;
      background: rgba(0,0,0,.5);
      border-radius: 50%;
      width: 50px;
      height: 50px;
      display: flex;
      justify-content: center;
      align-items: center;
      &::before{
          margin-left: 6px;
      }
    }
    .fa-pause{
      font-size: 24px;
      &::before{
        margin-left: 0;
      }
    }
  }
  &:hover {
    .play-controls{
      visibility: visible;
    }
  }
  .mask {
    position: absolute;
    right: 0;
    bottom: 0;
    height: 100%;
    z-index: 10;
    .toolbar {
      position: absolute;
      right: 8px;
      bottom: 8px;
      width: 40px;
      min-height: 10px;
      border-radius: 8px;
      background: rgba(117, 116, 116, 0.8);
      padding: 3px 0;
      overflow: hidden;
      z-index: 1;

      p {
        width: 100%;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        margin: 3px 0;
        border-bottom: 1px solid rgba(177, 175, 175, 0.5);
        font-size: 13px;
        i {
          font-size: 20px;
          cursor: pointer;
        }

        &:last-child {
          margin-bottom: 0;
          border-bottom: 0;
        }
      }
    }
  }

  &.full {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 99999999;
    background: rgba(33, 33, 33, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    max-width: unset;
    video {
      max-width: 100vw !important;
      height: 100vh !important;
      width: 100vw !important;
      max-height: unset;
    }

    .mask {
      display: flex;
      align-items: center;
      justify-items: center;
      justify-content: center;

      .toolbar {
        right: 30px;
        width: 60px;
        bottom: unset;
        background: rgba(49, 48, 48, 0.9);
        p {
          height: 50px;
        }
      }
    }
    .play-controls{
      i{
        width: 60px;
        height: 60px;
        font-size: 30px;
      }
      .fa-pause{
        font-size: 26px;
      }
    }
  }
}
</style>